package com.controller;

import com.entity.*;
import com.service.*;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Objects;

/**
 * @author liuronghua
 * @version 1.0
 * @data 2020-09-2020/9/22 9:09
 * @desc
 */
@Controller
@RequestMapping("/admin")
public class AdminController {
    @Resource
    private AdminService adminService;

    @Autowired
    private TypeService typeService;

    @Autowired
    private GoodService goodService;

    @Autowired
    private UserService userService;

    @Autowired
    private OrderService orderService;

    @GetMapping("/login")
    public String log() {return "/admin/login.jsp";}

    @PostMapping("/login")
    public String login(Admin admin, HttpServletRequest request, HttpSession session){
        Admin loginUser = adminService.getByUsernameAndPassword(admin.getUsername(),admin.getPassword());
        if (Objects.isNull(loginUser)){
            request.setAttribute("msg","用户名或密码错误");
            return "/admin/login.jsp";
        }
        session.setAttribute("admin",loginUser);
        //还原购物车
        return "/admin/index.jsp";
    }
    
    @GetMapping("/typeList")
    public String typeList(Model model){
        List<Types> list = typeService.getList();
        model.addAttribute("typeList",list);
        return "/admin/type_list.jsp";
    }



    @GetMapping("/goodList")
    public String goodList(Model model){
        List<Goods> list=goodService.getListAll();
        model.addAttribute("goodList",list);
        return "/admin/good_list.jsp";
    }

    @GetMapping("/adminList")
    public String adminList(Model model){
        List<Admin> list=adminService.getList();
        model.addAttribute("adminList",list);
        return "/admin/admin_list.jsp";
    }

    @GetMapping("/orderList")
    public String orderList(Model model){
        List<Order> orderList=orderService.getList();
        model.addAttribute("tabList",orderList);
        return "/admin/order_list.jsp";
    }
    

    @GetMapping("/userList")
    public String userList(Model model){
        List<Users> usersList=userService.getList();
        model.addAttribute("userList",usersList);
        return "/admin/user_list.jsp";
    }

    @GetMapping("/index")
    public String ind(){
        return "/admin/index.jsp";
    }

}
